<template>
  <div :class="{'hidden':hidden}" class="pagination-container">
    <a-pagination
      size="small" 
      :hideOnSinglePage="hidden"
      :current="currentPage"
      :pageSize="pageSize"
      :total="total"
      @showSizeChange="handleSizeChange"
      @change="handleCurrentChange"
    />
  </div>
</template>

<script>
export default {
  name: 'Pagination',
  props: {
    total: {
      required: true,
      type: Number
    },
    page: {
      type: Number,
      default: 0
    },
    limit: {
      type: Number,
      default: 10
    },
    pageSizes: {
      type: Array,
      default() {
        return [10, 20, 30, 50]
      }
    },
    layout: {
      type: String,
      default: 'total, sizes, prev, pager, next, jumper'
    },
    background: {
      type: Boolean,
      default: true
    },
    autoScroll: {
      type: Boolean,
      default: true
    },
    hidden: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    currentPage: {
      get() {
        return this.page + 1
      },
      set(val) {
        this.$emit('update:page', val - 1)
      }
    },
    pageSize: {
      get() {
        return this.limit
      },
      set(val) {
        this.$emit('update:limit', val)
      }
    }
  },
  methods: {
    handleSizeChange(page, pageSize) {
      this.$emit('pagination', { page: page - 1, limit: pageSize })     //因为后台接口currentPage是从0开始的
    },
    handleCurrentChange(current, size) {
      this.$emit('pagination', { page: current - 1, limit: size })
    }
  }
}
</script>

<style scoped>
  .pagination-container {
    /* background: #fff; */
    padding: 5px 16px;
  }
  .pagination-container.hidden {
    display: none;
  }
</style>
